package com.flycms.config;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

import javax.annotation.Resource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.flycms.interceptor.AdminInterceptor;
import com.flycms.interceptor.UserInterceptor;

/**
 * Open source house, All rights reserved
 * 开发公司：宏石<br/>
 * 版权：开源中国<br/>
 *
 * @author sun-kaifei
 * @version 1.0 <br/>
 * @email xxx@qq.com
 * @Date: 14:14 2020/7/8
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport{

	@Resource
	private AdminInterceptor interceptor;

	@Resource
	private UserInterceptor userInterceptor;

	@Override
	public void configurePathMatch(PathMatchConfigurer configurer) {
		configurer.setUseSuffixPatternMatch(false);
	}


	@Bean
	public LocaleResolver localeResolver() {
		SessionLocaleResolver slr = new SessionLocaleResolver();
		// 默认语言
		slr.setDefaultLocale(Locale.CHINA);
		return slr;
	}

	@Bean
	public LocaleChangeInterceptor localeChangeInterceptor() {
		LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
		// 参数名
		lci.setParamName("lang");
		return lci;
	}

	//添加拦截器
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(interceptor).addPathPatterns("/**/system/**")
				.excludePathPatterns("/*",
						"/**/system/login",
						"/**/system/logout",
						"/**/system/login_act");

		registry.addInterceptor(userInterceptor).addPathPatterns("/**/u/**","/question/add")
				.excludePathPatterns("/*",
						"/**/u/download/**",
						"/u/login",
						"/u/unauthorized",
						"/u/reg",
						"/u/mobilecode",
						"/u/addMobileUser",
						//退出登录
						"/u/logout",
						//登录处理
						"/rest/user/u/login_act",
						"/u/ajaxlogin",
						"/rest/user/u/reg_user",
						"/u/reset.json",
						"/u/logintip",
						"/u/mailcaptcha.json");
		registry.addInterceptor(localeChangeInterceptor());
	}

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**",
				"/*").addResourceLocations("classpath:/resources/",
				"file:./uploadfiles/",
				"file:./views/static/");
        super.addResourceHandlers(registry);
    }

    /**
     * 定义时间格式转换器
     * @return
     */
    @Bean
    public MappingJackson2HttpMessageConverter jackson2HttpMessageConverter() {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        SimpleModule module = new SimpleModule();
        module.addSerializer(BigDecimal.class, new ToStringSerializer());
        module.addSerializer(Long.class, new ToStringSerializer());
//        module.addSerializer(Integer.class, new JsonIntegerSerialize());
        mapper.registerModule(module);
        converter.setObjectMapper(mapper);
        return converter;
    }
    
    /**
     * 添加转换器
     * @param converters
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        //将我们定义的时间格式转换器添加到转换器列表中,
        //这样jackson格式化时候但凡遇到Date类型就会转换成我们定义的格式
        converters.add(jackson2HttpMessageConverter());
    }
}
